package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;

/**
 * @author spt
 * @description
 **/
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 预约设置-上传
     * @param orderSettingList  封装日期和预约数量的集合对象
     */
    @Override
    public void addAll(List<OrderSetting> orderSettingList) {
        log.warn("预约设置 批量添加 data {}",orderSettingList);
        //循环集合  逐条添加
        for (OrderSetting orderSetting : orderSettingList) {
            //1.根据日期查询  判断是否存在  存在则更新  不存在则插入
            Date orderDate = orderSetting.getOrderDate();
            int number = orderSetting.getNumber();
            OrderSetting order = orderSettingDao.selectByDate(orderDate);
            if (order != null) {
                //存在则更新
                orderSettingDao.updateById(order.getId(), number);
            }else {
                //不存在插入
                orderSettingDao.insert(new OrderSetting(orderDate,number));
            }
        }
    }

    /**
     *
     * @param year
     * @param month
     * @return  返回一个月内所有天数分别对应的数据。
     */
    @Override
    public List<Map<String,Object>> select(String year, String month) {
        int nian = Integer.parseInt(year);
        int yue = Integer.parseInt(month);

        //构造日期,从当月一号开始
        LocalDate start = LocalDate.of(nian,yue,1);//月份1号

        //LocalDate转换类型为Date
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = start.atStartOfDay(zoneId);
        Date kaishi = Date.from(zdt.toInstant());

        //拿到下个月1号
        Calendar c = Calendar.getInstance();
        c.setTime(kaishi);
        c.add(c.MONTH,1);      //转换为calendar +1月
        Date xiageyue = c.getTime();   //转换回Date类型

        List<Map<String,Object>> box = new ArrayList<>();   //存放返回数据的盒子
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        //执行到下个月第一天之前
        Calendar next = Calendar.getInstance();
        next.setTime(kaishi);
        while (kaishi.before(xiageyue)){             //日期在下个月1号之前为True。不在下个月1号之前为False，则跳出循环
            Integer ri = next.get(Calendar.DAY_OF_MONTH);
            Map<String,Object> map = new HashMap<>();//map集合接收三个需要返回的数据
            Integer i =orderSettingDao.selectGeshu(kaishi);
            if (i>0) {
                int p = orderSettingDao.selectNum(kaishi);//可预约人数
                int o = orderSettingDao.selectRes(kaishi);//已预约人数
                if(p<o){
                    map.put("date", ri);//日期的日
                    map.put("number", orderSettingDao.selectRes(kaishi));          //可预约人数
                    map.put("reservations", orderSettingDao.selectRes(kaishi));
                    //可预约人数小于已预约人数时，给可预约人数传入已预约人数的数值。
                }else {
                    map.put("date", ri);//日期的日
                    map.put("number", orderSettingDao.selectNum(kaishi));          //可预约人数
                    map.put("reservations", orderSettingDao.selectRes(kaishi));    //已预约人数
                }
                    log.info("map的数据:{}",map);
            }

            box.add(map);               //map集合存入list集合
//            System.out.println(kaishi+"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            next.add(c.DATE,1);//日期加一天
            kaishi=next.getTime();
//            System.out.println(kaishi+"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

            log.info("a的数据:{}",box);
        }
        return box;
    }

    /**
     *
     * @param orderSetting 修改基于日历实现预约设置
     */
    @Override
    public void editNumber(OrderSetting orderSetting) {

        Date orderDate = orderSetting.getOrderDate();//获取传入的时间
        int number = orderSetting.getNumber();       //获取传入要修改的值

        int oS = orderSettingDao.selectGeshu(orderDate);//设置判断数据库有无数据的条件
        int reservations =0;

        if (oS<1){//如果格子没有设置
            orderSettingDao.insertByDate(orderDate,number,reservations);
        }else {//格子设置数据，如果修改，人数不能低于已经预约了的人数
            int originNum = orderSettingDao.selectByDate(orderDate).getReservations();//获取已经预约了的人数数量

            if(number<originNum){
                throw new BusinessRuntimeException(MessageConst.ORDERSETTING_FAIL);
            }
            orderSettingDao.updateByDate(orderDate,number);
        }
    }

//    /**
//     * @param year  年
//     * @param month 月
//     * @return 返回查询数据
//     */
//    @Override
//    public List<OrderSetting> selectOrderSetting(Integer year, Integer month) {
//        //构建日期范围
//        String start = String.format("%d-%d-1", year, month);
//        String end = String.format("%d-%d-30", year, month);
//        List<OrderSetting> orderSettingList = orderSettingDao.selectOrderSetting(start, end);
//        return orderSettingList ;
//    }
//
//    /**
//     * 预约管理_可预约单独设置
//     *
//     * @param orderDate 日期
//     * @param number  可预约数量
//     * @return 返回成功失败
//     */
//    @Override
//    public void editNumberByDate(Date orderDate, int number) {
//        OrderSetting po = orderSettingDao.selectByDate(orderDate);
//        if (po!=null){
//            //存在则更新
//            orderSettingDao.updateById(po.getId(),number);
//        }else {
//            //不存在插入
//            orderSettingDao.insert(new OrderSetting(orderDate,number));
//        }
//    }
}
